00001 /*
00002  * This source code is a product of Sun Microsystems, Inc. and is provided
00003  * for unrestricted use.  Users may copy or modify this source code without
00004  * charge.
00005  *
00006  * SUN SOURCE CODE IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING
00007  * THE WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
00008  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
00009  *
00010  * Sun source code is provided with no support and without any obligation on
00011  * the part of Sun Microsystems, Inc. to assist in its use, correction,
00012  * modification or enhancement.
00013  *
00014  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
00015  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY THIS SOFTWARE
00016  * OR ANY PART THEREOF.
00017  *
00018  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
00019  * or profits or other special, indirect and consequential damages, even if
00020  * Sun has been advised of the possibility of such damages.
00021  *
00022  * Sun Microsystems, Inc.
00023  * 2550 Garcia Avenue
00024  * Mountain View, California  94043
00025  */
00026 
00027 /*
00028  * g711.c
00029  *
00030  * u-law, A-law and linear PCM conversions.
00031  */
00032 
00033 /*
00034  * December 30, 1994:
00035  * Functions linear2alaw, linear2ulaw have been updated to correctly
00036  * convert unquantized 16 bit values.
00037  * Tables for direct u- to A-law and A- to u-law conversions have been
00038  * corrected.
00039  * Borge Lindberg, Center for PersonKommunikation, Aalborg University.
00040  * bli@cpk.auc.dk
00041  *
00042  */
00043  
00044 #define SIGN_BIT        (0x80)          /* Sign bit for a A-law byte. */
00045 #define QUANT_MASK      (0xf)           /* Quantization field mask. */
00046 #define NSEGS           (8)             /* Number of A-law segments. */
00047 #define SEG_SHIFT       (4)             /* Left shift for segment number. */
00048 #define SEG_MASK        (0x70)          /* Segment field mask. */
00049 
00050 static int seg_aend[8] = {0x1F, 0x3F, 0x7F, 0xFF,
00051                             0x1FF, 0x3FF, 0x7FF, 0xFFF};
00052 static int seg_uend[8] = {0x3F, 0x7F, 0xFF, 0x1FF,
00053                             0x3FF, 0x7FF, 0xFFF, 0x1FFF};
00054 
00055 /* copy from CCITT G.711 specifications */
00056 unsigned char u2a[128] = {                      /* u- to A-law conversions */
00057         1,      1,      2,      2,      3,      3,      4,      4,
00058         5,      5,      6,      6,      7,      7,      8,      8,
00059         9,      10,     11,     12,     13,     14,     15,     16,
00060         17,     18,     19,     20,     21,     22,     23,     24,
00061         25,     27,     29,     31,     33,     34,     35,     36,
00062         37,     38,     39,     40,     41,     42,     43,     44,
00063         46,     48,     49,     50,     51,     52,     53,     54,
00064         55,     56,     57,     58,     59,     60,     61,     62,
00065         64,     65,     66,     67,     68,     69,     70,     71,
00066         72,     73,     74,     75,     76,     77,     78,     79,
00067 /* corrected:
00068         81,     82,     83,     84,     85,     86,     87,     88, 
00069    should be: */
00070         80,     82,     83,     84,     85,     86,     87,     88,
00071         89,     90,     91,     92,     93,     94,     95,     96,
00072         97,     98,     99,     100,    101,    102,    103,    104,
00073         105,    106,    107,    108,    109,    110,    111,    112,
00074         113,    114,    115,    116,    117,    118,    119,    120,
00075         121,    122,    123,    124,    125,    126,    127,    128};
00076 
00077 unsigned char a2u[128] = {                      /* A- to u-law conversions */
00078         1,      3,      5,      7,      9,      11,     13,     15,
00079         16,     17,     18,     19,     20,     21,     22,     23,
00080         24,     25,     26,     27,     28,     29,     30,     31,
00081         32,     32,     33,     33,     34,     34,     35,     35,
00082         36,     37,     38,     39,     40,     41,     42,     43,
00083         44,     45,     46,     47,     48,     48,     49,     49,
00084         50,     51,     52,     53,     54,     55,     56,     57,
00085         58,     59,     60,     61,     62,     63,     64,     64,
00086         65,     66,     67,     68,     69,     70,     71,     72,
00087 /* corrected:
00088         73,     74,     75,     76,     77,     78,     79,     79,
00089    should be: */
00090         73,     74,     75,     76,     77,     78,     79,     80,
00091 
00092         80,     81,     82,     83,     84,     85,     86,     87,
00093         88,     89,     90,     91,     92,     93,     94,     95,
00094         96,     97,     98,     99,     100,    101,    102,    103,
00095         104,    105,    106,    107,    108,    109,    110,    111,
00096         112,    113,    114,    115,    116,    117,    118,    119,
00097         120,    121,    122,    123,    124,    125,    126,    127};
00098 
00099 static int
00100 search(
00101         int             val,    /* changed from "short" *drago* */
00102         int *   table,
00103         int             size)   /* changed from "short" *drago* */
00104 {
00105         int             i;              /* changed from "short" *drago* */
00106 
00107         for (i = 0; i < size; i++) {
00108                 if (val <= *table++)
00109                         return (i);
00110         }
00111         return (size);
00112 }
00113 
00114 /*
00115  * linear2alaw() - Convert a 16-bit linear PCM value to 8-bit A-law
00116  *
00117  * linear2alaw() accepts an 16-bit integer and encodes it as A-law data.
00118  *
00119  *              Linear Input Code       Compressed Code
00120  *      ------------------------        ---------------
00121  *      0000000wxyza                    000wxyz
00122  *      0000001wxyza                    001wxyz
00123  *      000001wxyzab                    010wxyz
00124  *      00001wxyzabc                    011wxyz
00125  *      0001wxyzabcd                    100wxyz
00126  *      001wxyzabcde                    101wxyz
00127  *      01wxyzabcdef                    110wxyz
00128  *      1wxyzabcdefg                    111wxyz
00129  *
00130  * For further information see John C. Bellamy's Digital Telephony, 1982,
00131  * John Wiley & Sons, pps 98-111 and 472-476.
00132  */
00133 int linear2alaw(int     pcm_val)        /* 2's complement (16-bit range) */
00134                                         /* changed from "short" *drago* */
00135 {
00136         int             mask;   /* changed from "short" *drago* */
00137         int             seg;    /* changed from "short" *drago* */
00138         int             aval;
00139 
00140         pcm_val = pcm_val >> 3;
00141 
00142         if (pcm_val >= 0) {
00143                 mask = 0xD5;            /* sign (7th) bit = 1 */
00144         } else {
00145                 mask = 0x55;            /* sign bit = 0 */
00146                 pcm_val = -pcm_val - 1;
00147         }
00148 
00149         /* Convert the scaled magnitude to segment number. */
00150         seg = search(pcm_val, seg_aend, 8);
00151 
00152         /* Combine the sign, segment, and quantization bits. */
00153 
00154         if (seg >= 8)           /* out of range, return maximum value. */
00155                 return (0x7F ^ mask);
00156         else {
00157                 aval = seg << SEG_SHIFT;
00158                 if (seg < 2)
00159                         aval |= (pcm_val >> 1) & QUANT_MASK;
00160                 else
00161                         aval |= (pcm_val >> seg) & QUANT_MASK;
00162                 return (aval ^ mask);
00163         }
00164 }
00165 
00166 /*
00167  * alaw2linear() - Convert an A-law value to 16-bit linear PCM
00168  *
00169  */
00170 int alaw2linear(int     a_val)          
00171 {
00172         int             t;      /* changed from "short" *drago* */
00173         int             seg;    /* changed from "short" *drago* */
00174 
00175         a_val ^= 0x55;
00176 
00177         t = (a_val & QUANT_MASK) << 4;
00178         seg = ((unsigned)a_val & SEG_MASK) >> SEG_SHIFT;
00179         switch (seg) {
00180         case 0:
00181                 t += 8;
00182                 break;
00183         case 1:
00184                 t += 0x108;
00185                 break;
00186         default:
00187                 t += 0x108;
00188                 t <<= seg - 1;
00189         }
00190         return ((a_val & SIGN_BIT) ? t : -t);
00191 }
00192 
00193 #define BIAS            (0x84)          /* Bias for linear code. */
00194 #define CLIP            8159
00195 
00196 /*
00197  * linear2ulaw() - Convert a linear PCM value to u-law
00198  *
00199  * In order to simplify the encoding process, the original linear magnitude
00200  * is biased by adding 33 which shifts the encoding range from (0 - 8158) to
00201  * (33 - 8191). The result can be seen in the following encoding table:
00202  *
00203  *      Biased Linear Input Code        Compressed Code
00204  *      ------------------------        ---------------
00205  *      00000001wxyza                   000wxyz
00206  *      0000001wxyzab                   001wxyz
00207  *      000001wxyzabc                   010wxyz
00208  *      00001wxyzabcd                   011wxyz
00209  *      0001wxyzabcde                   100wxyz
00210  *      001wxyzabcdef                   101wxyz
00211  *      01wxyzabcdefg                   110wxyz
00212  *      1wxyzabcdefgh                   111wxyz
00213  *
00214  * Each biased linear code has a leading 1 which identifies the segment
00215  * number. The value of the segment number is equal to 7 minus the number
00216  * of leading 0's. The quantization interval is directly available as the
00217  * four bits wxyz.  * The trailing bits (a - h) are ignored.
00218  *
00219  * Ordinarily the complement of the resulting code word is used for
00220  * transmission, and so the code word is complemented before it is returned.
00221  *
00222  * For further information see John C. Bellamy's Digital Telephony, 1982,
00223  * John Wiley & Sons, pps 98-111 and 472-476.
00224  */
00225 int linear2ulaw( int    pcm_val)        /* 2's complement (16-bit range) */
00226 {
00227         int             mask;
00228         int             seg;
00229         int             uval;
00230 
00231         /* Get the sign and the magnitude of the value. */
00232         pcm_val = pcm_val >> 2;
00233         if (pcm_val < 0) {
00234                 pcm_val = -pcm_val;
00235                 mask = 0x7F;
00236         } else {
00237                 mask = 0xFF;
00238         }
00239         if ( pcm_val > CLIP ) pcm_val = CLIP;           /* clip the magnitude */
00240         pcm_val += (BIAS >> 2);
00241 
00242         /* Convert the scaled magnitude to segment number. */
00243         seg = search(pcm_val, seg_uend, 8);
00244 
00245         /*
00246          * Combine the sign, segment, quantization bits;
00247          * and complement the code word.
00248          */
00249         if (seg >= 8)           /* out of range, return maximum value. */
00250                 return (0x7F ^ mask);
00251         else {
00252                 uval = (seg << 4) | ((pcm_val >> (seg + 1)) & 0xF);
00253                 return (uval ^ mask);
00254         }
00255 
00256 }
00257 
00258 /*
00259  * ulaw2linear() - Convert a u-law value to 16-bit linear PCM
00260  *
00261  * First, a biased linear code is derived from the code word. An unbiased
00262  * output can then be obtained by subtracting 33 from the biased code.
00263  *
00264  * Note that this function expects to be passed the complement of the
00265  * original code word. This is in keeping with ISDN conventions.
00266  */
00267 int ulaw2linear( int    u_val)
00268 {
00269         int t;
00270 
00271         /* Complement to obtain normal u-law value. */
00272         u_val = ~u_val;
00273 
00274         /*
00275          * Extract and bias the quantization bits. Then
00276          * shift up by the segment number and subtract out the bias.
00277          */
00278         t = ((u_val & QUANT_MASK) << 3) + BIAS;
00279         t <<= (u_val & SEG_MASK) >> SEG_SHIFT;
00280 
00281         return ((u_val & SIGN_BIT) ? (BIAS - t) : (t - BIAS));
00282 }
00283 
00284 #if 0
00285 
00286 /* A-law to u-law conversion */
00287 static int alaw2ulaw (int       aval)
00288 {
00289         aval &= 0xff;
00290         return ((aval & 0x80) ? (0xFF ^ a2u[aval ^ 0xD5]) :
00291             (0x7F ^ a2u[aval ^ 0x55]));
00292 }
00293 
00294 /* u-law to A-law conversion */
00295 static int ulaw2alaw (int       uval)
00296 {
00297         uval &= 0xff;
00298         return ((uval & 0x80) ? (0xD5 ^ (u2a[0xFF ^ uval] - 1)) :
00299             (0x55 ^ (u2a[0x7F ^ uval] - 1)));
00300 }
00301 
00302 #endif
00303 